package webapp.frontOffice.servlet;

import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import modele.contrat.Action;
import modele.contrat.Contrat;
import modele.contrat.OptionC;
import modele.gestionVente.Enchere;
import modele.gestionVente.VenteDirecte;
import modele.utilisateur.Entreprise;
import modele.utilisateur.MembreSociete;
import modele.utilisateur.Utilisateur;
import dao.contrat.ActionDAO;
import dao.contrat.OptionCDAO;
import dao.gestionVente.EnchereDAO;
import dao.gestionVente.VenteDirecteDAO;

/**
 * Servlet implementation class ActionCreer
 */
@WebServlet("/masociete/creeraction")
public class ActionCreer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/viewFrontoffice/actioncreer.jsp");
        dispatcher.forward(request, response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		//Var session init
		HttpSession session = request.getSession();
		Utilisateur user = (Utilisateur)(MembreSociete) session.getAttribute("user");
		Entreprise entreprise = ((MembreSociete) session.getAttribute("user")).getEntreprise();
		
		//Var POST init
		String typeContrat = request.getParameter("typecontrat");
		String typeVente = request.getParameter("typevente");
		double prix = Double.parseDouble(request.getParameter("prix"));
		Date dateEchance;
		
        String[] temp = request.getParameter("dateecheance").split("/");
        dateEchance = new GregorianCalendar(Integer.parseInt(temp[2]), Integer.parseInt(temp[1])-1, Integer.parseInt(temp[0])).getTime();
        
        //Var new init
        Contrat contrat = null;
        Action action = null;
        OptionC option = null;
        Enchere enchere = null;
        VenteDirecte vente = null;
        
        if(typeContrat.equals("action"))
        {
        	action = new Action();
        	action.setEntrepriseOrigine(entreprise);
        	action.setPossesseur(user);
        	contrat = action;
        }
        else if(typeContrat.equals("option"))
        {
        	option = new OptionC();
        	option.setEntrepriseOrigine(entreprise);
        	option.setPossesseur(user);
        	contrat = option;
        }
        
        
        if(typeVente.equals("enchere"))
        {
        	enchere = new Enchere();
        	enchere.setDate_echeance(dateEchance);
        	enchere.setDate_emission(new Date());
        	enchere.setPrixEmission(prix);
        	enchere.setVendeur(user);
        	
        	Set<Contrat> portefeuille = new HashSet<Contrat>();
        	portefeuille.add(contrat);
        	enchere.setPortefeuille(portefeuille);
        	
        }
        else if(typeVente.equals("vente"))
        {
        	vente = new VenteDirecte();
        	vente.setDate_echeance(dateEchance);
        	vente.setDate_emission(new Date());
        	vente.setPrix(prix);
        	vente.setVendeur(user);
        	
        	Set<Contrat> portefeuille = new HashSet<Contrat>();
        	portefeuille.add(contrat);
        	vente.setPortefeuille(portefeuille);
        }
        
        if(action != null)	new ActionDAO().create(action);
        if(option != null)	new OptionCDAO().create(option);
        if(vente != null)	new VenteDirecteDAO().create(vente);
        if(enchere != null)	new EnchereDAO().create(enchere);
        
        
        response.sendRedirect(request.getContextPath() + "/masociete?success=1");
	}
}
